package com.mybatis.mybatisdatab.controller;

import com.mybatis.mybatisdatab.mapper.UserMapper;
import com.mybatis.mybatisdatab.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("queryUserList")
    public List<User> queryUserList(){
        List<User> userList = userMapper.queryUserList();
        for (User user : userList){
            System.out.println(user);
        }
        return userList;

    }


    @GetMapping("queryUserById/{id}")
    public User queryUserById(@PathVariable("id") int id){
        User user = userMapper.queryUserById(id);
        return user;

    }

    @GetMapping("queryUserByName/{name}")
    public List<User> queryUserById(@PathVariable("name") String name){
        List<User> userList = userMapper.queryUserByName(name);
        return userList;

    }

    @GetMapping("addUser/{id}")
    public String addUser(@PathVariable("id") int id){
        User user = new User();

        Random random = new Random();
        user.setId(id);
        user.setName("新增展示官方"+id);
        user.setPwd("123456");

        userMapper.addUser(user);

        return "ok";

    }


    @GetMapping("updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        User user = new User();
        Random random = new Random();
        user.setId(id);
        user.setName("我这里来显示：展示官方");
        user.setPwd("123456");
        userMapper.updateUser(user);

        return "ok";

    }


    @GetMapping("deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){

        userMapper.deleteUser(id);
        return "ok";

    }

    @RequestMapping("/getIndex")
    public String getIndex(){

        return "index";
    }
}
